<template>
	<view class="agent-pages-mine" :style="{background:!configInfo.plugAuth.agentport?pageColor:''}">

		<load-more :noMore="true" :loading="true" v-if="!isLoad">
		</load-more>

		<block v-if="isLoad">
			<block v-if="!is_network">
				<abnor type="REQUEST_ERROR" @confirm="initRefresh" btnSize="" :isCenter="true">
				</abnor>
			</block>
			<block v-if="is_network">

				<!-- #ifndef H5 -->
				<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" title="首页" color="#000"
					:backgroundColor="pageScrollTop>1?'#D4FADB':''">
				</uni-nav-bar>
				<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
				<!-- #endif -->

				<block v-if="!configInfo.plugAuth.agentport">
					<abnor type="NOT_AUTH" :isCenter="true"></abnor>
				</block>
				<block v-if="configInfo.plugAuth.agentport">

					<abnor @confirm="$util.toLoginOut(3)"
						:tip="[{text: options.agent ? `您还不是${$t('action.agentName')}哦～`:'您还不是管理员哦~'}]"
						:button="[{ text: '退出登录', type: 'confirm' }]" btnSize="" :isCenter="true"
						v-if="detail.id == -1"></abnor>

					<block v-else>

						<block
							v-if="(!options.agent&&detail.is_agent&&((!detail.agent_coach_auth && (!configInfo.plugAuth.store || !detail.store_auth)) || (detail.agent_coach_auth && (!configInfo.plugAuth.store || !detail.store_auth) && detail.node.length == 1 && detail.node[0] == 'MarketCoupHxrecord') || (configInfo.plugAuth.store && detail.store_auth && !detail.agent_coach_auth && detail.node.length == 1 && detail.node[0] != 'MarketCoupHxrecord')))||
							(options.agent && !detail.agent_coach_auth && (!configInfo.plugAuth.channel || !detail.channel_auth || !configInfo.channel_check_status) && (!configInfo.plugAuth.salesman || !detail.salesman_auth  || !configInfo.salesman_check_status) && (!configInfo.plugAuth.reseller || !detail.reseller_auth) && !detail.sub_agent_auth)">
							<abnor type="NOT_AUTH" :isCenter="true"></abnor>
						</block>
						<block v-else>
							<image mode="aspectFill" lazy-load class="mine-bg abs"
								src="https://api.huixuananmm.com/admin/anmo/adminport/home-bg.png"></image>
							<view class="rel" style="padding:0 20rpx">
								<!-- #ifdef H5 -->
								<view style="height:1rpx"></view>
								<!-- #endif -->
								<view class="flex-between pt-lg pb-sm"
									v-if="configInfo.plugAuth.store && detail.store_auth && (options.agent || detail.is_agent) && detail.store_num*1>0">
									<view class="flex-center f-mini-title c-title" style="width:560rpx">
										当前门店：
										<view class="flex-1 flex-y-center text-bold">
											<view class="ellipsis"
												:style="{maxWidth:detail.store_info.status!=1?'300rpx':'410rpx'}">
												{{detail.store_info.title}}
											</view>
											<view class="del-tag f-desc text-normal ml-sm"
												v-if="detail.store_info.status!=1">
												{{detail.store_info.status==0?'已下架':'已删除'}}
											</view>
										</view>
									</view>
									<view
										@tap.stop="$util.goUrl({url:`/agent/pages/shopstore/choose-store?id=${detail.store_info.store_id}&agent=${options.agent}`})"
										class="store-btn fill-base flex-center f-desc text-bold"
										:style="{color:primaryColor}" v-if="detail.store_num*1>1">切换门店
									</view>
								</view>

								<view class="fill-base mt-md radius-20"
									v-if="(!options.agent && !detail.is_agent) || ((options.agent || (detail.is_agent && (detail.node.length>1 || (detail.node.length == 1 && detail.node[0] != 'MarketCoupHxrecord')))) && detail.agent_coach_auth)">
									<view class="pd-lg f-paragraph c-black text-bold">智能助手</view>
									<view @tap.stop="goOrder(index)" class="notice-info flex-center pl-lg pr-md pb-lg"
										v-for="(item,index) in detail.notice" :key="index">
										<view class="title flex-center f-icontext c-base mr-md"
											:style="{background:item.type==1?primaryColor: item.type==2?'#FFA229':'#E82F21'}">
											{{noticeType[item.type].title}}
										</view>
										<view class="flex-1 flex-between f-desc">
											<view class="c-title">{{item.id ? noticeType[item.type].text : '暂无数据'}}
											</view>
											<view class="flex-y-center c-caption" v-if="item.id">点击查看<i
													class="iconfont icon-right ml-sm"></i>
											</view>
										</view>
									</view>
								</view>


								<view @tap.stop="toHxOrder" class="fill-base flex-between mt-md radius-20"
									style="padding:30rpx 20rpx"
									v-if="configInfo.plugAuth.store&&detail.store_auth&&detail.group_write_off_auth&&(options.agent || (detail.is_agent && detail.node.includes('MarketCoupHxrecord')))">
									<view class="flex-center">
										<view class="scan-info flex-center"><i class="iconfont iconhexiao"
												:style="{color:primaryColor}"></i>
										</view>
										<view class="f-mini-title c-title text-bold">核销订单</view>
									</view>
									<i class="iconfont icon-right c-caption"></i>
								</view>


								<view class="fill-base mt-md radius-20"
									v-if="options.agent && (detail.agent_coach_auth  || (configInfo.plugAuth.channel && detail.channel_auth && configInfo.channel_check_status) || (configInfo.plugAuth.salesman && detail.salesman_auth && configInfo.salesman_check_status) || (configInfo.plugAuth.reseller && detail.reseller_auth) || detail.sub_agent_auth)">
									<view class="pd-lg f-paragraph c-black text-bold">功能管理</view>
									<view class="tool-list flex-warp pl-lg pb-sm">
										<view @tap.stop="toJump('toolList',index)"
											class="tool-item flex-center fill-base mb-md mr-md radius-24"
											v-for="(item,index) in toolList" :key="index"
											v-show="(item.auth && detail[item.auth]) || (item.key && configInfo.plugAuth[item.key] && detail[`${item.key}_auth`] && (item.key=='reseller' || (item.key!='reseller'&& configInfo[`${item.key}_check_status`])))">
											<view class="item-img flex-center radius" :style="{background:rgbaColor}">
												<i class="iconfont" :class="item.icon"
													:style="{color:primaryColor}"></i>
											</view>
											<view class="flex-1 ml-md f-paragraph">{{item.text}}</view>
										</view>
									</view>
								</view>
							</view>
						</block>
					</block>
					<view class="space-lg"></view>
					<view :style="{height: `${configInfo.tabbarHeight}px`}"></view>
					<longbingbing-tabbar tabType="agentTabBar" :cur="1"></longbingbing-tabbar>
				</block>
			</block>
		</block>
		<!-- #ifdef MP-WEIXIN -->
		<user-privacy ref="user_privacy" :show="false"></user-privacy>
		<!-- #endif -->

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				isLoad: false,
				options: {},
				is_network: true,
				rgbaColor: '',
				noticeType: {
					1: {
						title: '订单通知',
						text: '您有新的订单来啦!'
					},
					2: {
						title: '退款通知',
						text: '您有新的客户退款通知!'
					},
					3: {
						title: '拒单通知',
						text: '有' + this.$t('action.attendantName') + '拒单, 请尽快处理!'
					},
				},
				toolList: [{
						icon: 'icondaizhuandan',
						text: this.$t('action.attendantName') + '管理',
						url: '/agent/pages/technician/list',
						auth: 'agent_coach_auth'
					}, {
						icon: 'iconbangdingqudaoshang',
						text: '绑定' + this.$t('action.channelName'),
						url: '/agent/pages/poster/channel',
						key: 'channel'
					},
					{
						icon: 'iconbangdingyewuyuan',
						text: '绑定业务员',
						url: '/agent/pages/poster/salesman',
						key: 'salesman'
					},
					{
						icon: 'iconbangdingfenxiaoyuan1',
						text: '绑定' + this.$t('action.resellerName'),
						url: '/agent/pages/poster/distribution',
						key: 'reseller'
					},
					{
						icon: 'iconbangdingdailishang',
						text: '绑定' + this.$t('action.agentName'),
						url: '/agent/pages/poster/agent',
						auth: 'sub_agent_auth'
					}
				],
			}
		},
		computed: mapState({
			configInfo: state => state.config.configInfo,
			detail: state => state.user.agentInfo,
			haveOperItem: state => state.order.haveOperItem,
			agentportType: state => state.user.agentportType,
		}),
		async onLoad(options) {
			let isHave = options.hasOwnProperty('agent')
			let agent = isHave ? options.agent : this.agentportType
			options.agent = agent * 1
			this.options = options
			if (isHave) {
				this.updateUserItem({
					key: 'agentportType',
					val: options.agent * 1
				})
			}
			this.$util.setNavigationBarColor({
				bg: '#ffffff',
				color: '#000000'
			})
			// #ifdef APP-PLUS 
			let isIos = uni.getDeviceInfo().system.includes('iOS')
			if (isIos) {
				let nt = plus.networkinfo.getCurrentType()
				if ([0, 1].includes(nt)) {
					console.log("无网络")
					this.is_network = false
					this.isLoad = true
					this.loading = false
					this.$util.hideAll()
				} else {
					console.log("有网络")
					this.initIndex()
				}
			} else {
				this.initIndex()
			}
			// #endif
			// #ifndef APP-PLUS
			this.initIndex()
			// #endif
		},
		async onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			await this.initRefresh()
			uni.stopPullDownRefresh()
		},
		methods: {
			...mapActions(['getConfigInfo', 'getAgentInfo', ]),
			...mapMutations(['updateUserItem']),
			async initIndex(refresh = false) {
				await this.getConfigInfo()
				this.rgbaColor = this.$util.rgbColor(this.primaryColor, 0.15)
				await this.getAgentInfo()
				let login_auth = await this.checkLoginAuth()
				console.log(login_auth, "===login_auth")
				if (!login_auth) {
					this.updateUserItem({
						key: 'userInfo',
						val: {}
					})
					this.updateUserItem({
						key: 'autograph',
						val: ''
					})
					this.$util.goUrl({
						url: '/agent/pages/login',
						openType: 'reLaunch'
					})
					console.log("非内部人员不可登录")
					return
				}

				this.is_network = true
				this.isLoad = true
				this.$util.hideAll()


				// #ifdef MP-WEIXIN
				let privacyCheck = this.$refs.user_privacy.check()
				if (privacyCheck) {
					this.$refs.user_privacy.open()
					return
				}
				// #endif
				// #ifdef H5
				if (!refresh) {
					this.$jweixin.hideOptionMenu()
				}
				// #endif

			},
			initRefresh() {
				this.initIndex(true)
			},
			async checkLoginAuth() {
				let {
					agent_status = 0,
						admin_status = 0
				} = this.userInfo
				let {
					agentportType
				} = this
				let login_auth = true
				// #ifdef MP-WEIXIN-AGENT
				console.log(11111112, "=====checkLoginAuth")
				if ((!agent_status && agentportType) || (!admin_status && !agentportType)) {
					login_auth = false
				}
				// #endif
				// #ifdef APP-PLUS
				if (this.is_app_user == 3 && ((!agent_status && agentportType) || (!admin_status && !agentportType))) {
					login_auth = false
				}
				// #endif
				return login_auth
			},
			async goOrder(index) {
				let {
					id = 0,
						order_id = 0,
						type
				} = this.detail.notice[index]
				if (!order_id) return
				let {
					agent
				} = this.options
				let methodKey = agent ? 'agent' : 'admin'
				await this.$api[methodKey].noticeUpdate({
					id,
					have_look: 1,
					is_pop: 1
				})
				this.initRefresh()
				let page = type == 2 ? 'refund' : 'order'
				let url = type === 3 ? `/agent/pages/order/change?id=${order_id}&agent=${agent}` :
					`/agent/pages/${page}/detail?id=${order_id}&agent=${agent}`
				this.$util.goUrl({
					url
				})
			},
			async toHxOrder() {
				let that = this
				let {
					hxOrder
				} = this
				// #ifdef H5
				let result = await that.$jweixin.scanQRCode(hxOrder)
				this.hxOrder = 1
				window.location.href = result
				// #endif
				// #ifndef H5
				uni.scanCode({
					success: function(res) {
						that.$util.hideAll()
						that.$util.goUrl({
							url: `/${res.path}`
						})
					}
				})
				// #endif
			},
			toJump(key, index) {
				let {
					id = 0,
						url,
						text
				} = this[key][index]
				let {
					agent
				} = this.options
				let joinKey = url.includes('?') ? '&' : '?'
				url += `${joinKey}agent=${agent}`
				this.$util.goUrl({
					url
				})
			}
		}
	}
</script>

<style lang="scss">
	.agent-pages-mine {
		.mine-bg {
			/* #ifdef H5 */
			top: -180rpx;
			/* #endif */
			/* #ifndef H5 */
			top: 0;
			/* #endif */
			width: 100%;
			height: 550rpx;
			z-index: 0;
		}


		.icon-right {
			font-size: 20rpx;
		}

		.del-tag {
			height: 32rpx;
			line-height: 32rpx;
			padding: 0 4rpx;
			text-align: center;
			border-radius: 4rpx;
			color: #E82F21;
			border: 1rpx solid #E82F21;

			.iconfont {
				font-size: 40rpx;
			}
		}

		.store-btn {
			width: 130rpx;
			height: 52rpx;
			border-radius: 8rpx;
		}

		.notice-info {
			.title {
				width: 100rpx;
				height: 34rpx;
				border-radius: 4rpx;
			}
		}

		.scan-info {
			width: 80rpx;
			height: 80rpx;
			background: #FFFFFF;
			box-shadow: 0px 3rpx 31rpx -2rpx rgba(217, 224, 219, 0.5);
			border-radius: 29rpx;
			margin-right: 16rpx;

			.iconfont {
				font-size: 48rpx;
			}
		}

		.tool-list {
			.tool-item {
				width: calc(50% - 25rpx);
				height: 120rpx;
				padding: 0 24rpx;
				box-shadow: 0px 3px 31px -2px rgba(217, 224, 219, 0.5);

				.item-img {
					width: 80rpx;
					height: 80rpx;

					.iconfont {
						font-size: 50rpx;
					}
				}

				.f-paragraph {
					color: #0F0D0E;
				}
			}
		}
	}
</style>